package com.kvest.sqlitetest;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.kvest.sqlitetest.datastorage.sqlitestorage.MultithreadingSQLiteManager;
import com.kvest.sqlitetest.datastorage.sqlitestorage.TestHelper;
import com.kvest.sqlitetest.datastorage.sqlitestorage.table.ProductTable;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 07.07.13
 * Time: 22:34
 * To change this template use File | Settings | File Templates.
 */
public class ProductsListActivity extends Activity {
    public static final String STORAGE_TYPE_EXTRA = "com.kvest.sqlitetest.STORAGE_TYPE";
    public static final int STORAGE_TYPE_SIMPLE = 1;
    public static final int STORAGE_TYPE_MULTITHREAD = 2;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        ListView list = new ListView(this);

        int storageType = getIntent().getIntExtra(STORAGE_TYPE_EXTRA, STORAGE_TYPE_SIMPLE);

        Cursor products = null;
        if (storageType == STORAGE_TYPE_SIMPLE) {
            TestHelper helper = new TestHelper(this);
            products = helper.getTableCursorWitoutClosingDB(ProductTable.TABLE_NAME);
        } else if (storageType == STORAGE_TYPE_MULTITHREAD) {
            products = MultithreadingSQLiteManager.getInstance().getTableCursor(ProductTable.TABLE_NAME);
        }
        if (products != null) {
            startManagingCursor(products);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.product_item, products,
                                                            new String[]{ProductTable.NAME_COLUMN, ProductTable.PID_COLUMN},
                                                            new int[]{R.id.product_name_tv, R.id.product_pid_tv});

            list.setAdapter(adapter);
            Log.d("KVEST_TAG", "count=" + adapter.getCount());
        }

        setContentView(list);
    }
}
